package demo1;

import demo2.Cell;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Tetris extends JPanel {
    SoundPlayer soundPlayer = new SoundPlayer();

    public static BufferedImage I;
    public static BufferedImage J;
    public static BufferedImage L;
    public static BufferedImage O;
    public static BufferedImage S;
    public static BufferedImage T;
    public static BufferedImage Z;
    public static BufferedImage backImage;

    static {
        try {
            I = ImageIO.read(new File("images/I.png"));
            J = ImageIO.read(new File("images/J.png"));
            L = ImageIO.read(new File("images/L.png"));
            O = ImageIO.read(new File("images/O.png"));
            S = ImageIO.read(new File("images/S.png"));
            T = ImageIO.read(new File("images/T.png"));
            Z = ImageIO.read(new File("images/Z.png"));
            backImage = ImageIO.read(new File("images/1.jpg"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(backImage, 0, 0, null);
        g.translate(22, 15);
        Draw.paintWall(g);
        Draw.paintCurrentOne(g);
        Draw.paintNextOne(g);
        Draw.paintScore(g);
        Draw.paintState(g);
    }

    public void start() {
        Statement.game_state = Statement.PLAYING;
        KeyListener l = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                switch (code) {
                    case KeyEvent.VK_Q:
                        CellMethod.sortDropAction();
                        soundPlayer.playSound(Statement.SP_KEY);
                        break;
                    case KeyEvent.VK_A:
                        CellMethod.moveLeftAction();
                        soundPlayer.playSound(Statement.SP_KEY);
                        break;
                    case KeyEvent.VK_D:
                        CellMethod.moveRightAction();
                        soundPlayer.playSound(Statement.SP_KEY);
                        break;
                    case KeyEvent.VK_W:
                        CellMethod.rotateRightAction();
                        soundPlayer.playSound(Statement.SP_KEY);
                        break;
                    case KeyEvent.VK_S:
                        CellMethod.rotateLeftAction();
                        soundPlayer.playSound(Statement.SP_KEY);
                        break;
                    case KeyEvent.VK_SPACE:
                        CellMethod.handDropAction();
                        soundPlayer.playSound(Statement.SP_KEY);
                        break;
                    case KeyEvent.VK_P:
                        soundPlayer.playSound(Statement.SP_KEY);
                        if (Statement.game_state == Statement.PLAYING) {
                            Statement.game_state = Statement.PAUSE;
                        }
                        break;
                    case KeyEvent.VK_C:
                        soundPlayer.playSound(Statement.SP_KEY);
                        if (Statement.game_state == Statement.PAUSE) {
                            Statement.game_state = Statement.PLAYING;
                        }
                        break;
                    case KeyEvent.VK_R:
                        soundPlayer.playSound(Statement.SP_KEY);
                        Statement.game_state = Statement.PLAYING;
                        Statement.wall = new Cell[18][9];
                        Statement.currentOne = Tetromino.randomOne();
                        Statement.nextOne = Tetromino.randomOne();
                        Statement.totalScore = 0;
                        Statement.totalLine = 0;
                        break;
                    default:
                }
            }
        };

        // 将俄罗斯方块窗口设置为焦点
        SwingUtilities.invokeLater(() -> {
            this.addKeyListener(l);
            this.setFocusable(true);
            this.requestFocusInWindow();
        });

        // 使用Swing Timer代替while(true)循环
        Timer timer = new Timer(500, e -> {
            if (Statement.game_state == Statement.PLAYING) {
                JudgeMethod.judge();
            }
            repaint();
        });
        timer.start();
    }
}
